﻿// ***********************************************************************
// Assembly         : DonNicky.Common
// Author           : donnicky
// Created          : 11-19-2012
//
// Last Modified By : donnicky
// Last Modified On : 12-02-2012
// ***********************************************************************
using System;
#if NET4 || NET45
using System.ComponentModel;
#endif
using System.Linq;
using System.Reflection;

namespace DonNicky.Common
{
    /// <summary>
    /// Type extensions class.
    /// </summary>
    public static class TypeExtensions
    {
        /// <summary>
        /// Gets the display name of the type as defined in DisplayNameAttribute/TypeDisplayNameAttribute.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>The display name.</returns>
        // ReSharper disable MemberCanBePrivate.Global
        public static string GetDisplayName(this Type type)
        // ReSharper restore MemberCanBePrivate.Global
        {
#if NET4 || NET45
            if (type.GetCustomAttributes(typeof(DisplayNameAttribute), true).Any())
                // ReSharper disable PossibleNullReferenceException
                return (type.GetCustomAttributes(typeof(DisplayNameAttribute), true).Single() as DisplayNameAttribute).DisplayName;
            // ReSharper restore PossibleNullReferenceException
#endif
            var attr = type.GetAttribute<TypeDisplayNameAttribute>();
            if (attr != null)
                return attr.ResourceType == null ? attr.Name : attr.ResourceType.GetResourceValue(attr.Name);
            return type.Name;
        }

        /// <summary>
        /// Gets the display name (in plural form) of the type as defined in LocalizedDisplayNameAttribute/TypeDisplayNameAttribute.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>The display name in plural form.</returns>
        public static string GetPluralDisplayName(this Type type)
        {
#if NET4 || NET45
            if (type.GetCustomAttributes(typeof(LocalizedDisplayNameAttribute), true).Any())
                // ReSharper disable PossibleNullReferenceException
                return (type.GetCustomAttributes(typeof(LocalizedDisplayNameAttribute), true).Single() as LocalizedDisplayNameAttribute).PluralDisplayName;
            // ReSharper restore PossibleNullReferenceException
#endif
            var attr = type.GetAttribute<TypeDisplayNameAttribute>();
            if (attr != null)
                return attr.ResourceType == null ? attr.PluralName : attr.ResourceType.GetResourceValue(attr.PluralName);

            return type.GetDisplayName();
        }
        /// <summary>
        /// Gets the string resource value.
        /// </summary>
        /// <param name="resourceType">Type of the resource.</param>
        /// <param name="resourceName">Name of the resource.</param>
        /// <returns>The resource value.</returns>
        public static string GetResourceValue(this Type resourceType, string resourceName)
        {
            foreach (var staticProperty in resourceType.GetProperties(BindingFlags.Static | BindingFlags.Public).Where(staticProperty => staticProperty.PropertyType == typeof(string) && staticProperty.Name == resourceName))
                return staticProperty.GetValue(null, null) as string;

            return resourceName;
        }
    }
}
